$(function () {

    /*选择器抽取的变量*/
    var pro_datagrid = $("#pro_datagrid");
     var pro_dialog = $("#pro_dialog");
    var pro_form = $("#pro_form");
    var pro_pwddialog = $("#pro_pwddialog");
    var password_form = $("#password_form");


    /*easyui的数据列表组件初始化*/
    pro_datagrid.datagrid({
        title: "商品管理",
        url: "/proEchart/list.do",
        fit: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: "#pro_toolbar",
        onSelect: function (index, row) {
            if (row.state) {
                $("#btn_state").linkbutton({
                    text: "离职"
                })
            } else {
                $("#btn_state").linkbutton({
                    text: "复职"
                })
            }
        },
        columns: [[
            {field: 'sn', title: '商品条码', width: 100, checkbox: true},
            {field: 'name', title: '商品名称', width: 100},
            {field: 'number', title: '销售数', width: 100},
            {field: 'salePrice', title: '销售金额', width: 100, align: 'right'},
            {field: 'profit', title: '销售毛利', width: 100, align: 'right'},
        ]]
    })

    /*弹出编辑框的初始化*/
    pro_dialog.dialog({
        width: 400,
        height: 350,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["save"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["cancel"]();
            }
        }],
        onClose: function () {
            pro_form.form('clear');
        }
    })

    /*修改密码弹出框*/
    pro_pwddialog.dialog({
        width: 300,
        height: 115,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["savepassword"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["pwdcancel"]();
            }
        }],
        onClose: function () {
            password_form.form('clear');
        }
    })



    /*事件对象*/
    var methodObj = {
        reload: function () {
            window.location.reload();
        },

        /*状态改变事件*/
        changeState: function () {
            var row = pro_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "error");
                return;
            }
            $.messager.confirm("温馨提示", '确定要改变状态吗?', function (tr) {
                if (tr) {
                    $.post("/employee/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "question", function () {
                                pro_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error");
                        }
                    })
                }
            })
        },


        /*表单取消事件*/
        cancel: function () {
            pro_dialog.dialog('close');
        },

        /*高级查询事件*/
        query: function () {
            var keyword = $("#keyword").textbox('getValue');
            var beginDate = $("#beginDate").datebox('getValue');
            var endDate = $("#endDate").datebox('getValue');

            pro_datagrid.datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate,
            })
        },


        /*导出事件*/
        export: function () {
            window.open("/client/exportData.do?"+$("#pro_queyForm").serialize());
        },

        importCancel:function () {
            $("#pro_importData").dialog('close');
        }

    }

    /*统一绑定事件*/
    $("a[data-method]").click(function () {
        var method = $(this).data("method");
        methodObj[method]();
    })

})
